var YHTimePicker = require('/yhTimePicker.js');
var timepicker = new YHTimePicker(),
    // dateArray = ["现在", "今天", "明天", "后天"],
    dateArray = ["今天", "明天", "后天"],
    selectDate = {},
    hoursArray = [],
    minutesArray = [],
    objectDateRange = [],
    coulmn = 0,
    dateIndex = 0,
    firstDayDate = new Date(),
    value = 0;
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        ComfrimYHDatePickerFunc: {
            type: Function,
            value: function() {}
        },
        Title: {
            type: String,
            value: '请选择时间'
        },
        HasNow: {
            type: Boolean,
            value: false,
            observer: function(newval, oldval) {
                // console.log(`newval = ${newval}    oldval = ${oldval}`);
            }
        },
        FirstTimeMinuteLatter: {
            type: Number,
            value: 10
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        dateTime: '',
        dateRange: [],
        dateRangeIndex: [0, 0, 0]
    },
    ready: function() {
        this.setData({
            dateTime: this.properties.Title
        });
        // init parameter
        dateArray = [],
            selectDate = {},
            hoursArray = [],
            minutesArray = [],
            objectDateRange = [],
            coulmn = 0,
            dateIndex = 0,
            value = 0;
        // init date picker

        // testToday
        // const today = new Date(2019, 6, 31, 21, 16),

        const today = new Date(),
            todayTimestamp = today.getTime();

        // var dateArr = ["现在"];
        var dateArr = this.properties.HasNow ? ["现在"] : [];
        const weekArr = ["星期日  ", "星期一  ", "星期二  ", "星期三  ", "星期四  ", "星期五  ", "星期六  "];
        const spaceTime = this.properties.FirstTimeMinuteLatter;
        firstDayDate = new Date(todayTimestamp + spaceTime * 60 * 1000);

        const minuteR = Math.ceil(firstDayDate.getMinutes() / 10) * 10;
        const showDate = new Date(firstDayDate.getFullYear(), firstDayDate.getMonth(), firstDayDate.getDate(), firstDayDate.getHours(), minuteR);
        const showDateTimeStamp = showDate.getTime();

        const todayDayFlag = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0);
        const showDateFlag = new Date(showDate.getFullYear(), showDate.getMonth(), showDate.getDate(), 0, 0);

        var spaceDayNum = Math.floor(((showDateFlag.getTime() - todayDayFlag.getTime()) / (24 * 60 * 60 * 1000)));

        // init day cloum 
        const letterDateStrs = ["今天  ", "明天  ", "后天  "];
        for (var i = spaceDayNum; i < (7 + spaceDayNum); i++) {
            let initPar = (i - spaceDayNum);
            let dateStr;
            let date = new Date(showDateTimeStamp + initPar * 24 * 60 * 60 * 1000);

            if (initPar <= 2) {
                dateStr = letterDateStrs[initPar] + (date.getMonth() + 1) + "月" + date.getDate() + "日";

            } else {
                dateStr = weekArr[date.getDay()] + (date.getMonth() + 1) + "月" + date.getDate() + "日";
            }


            dateArr.push(dateStr);
        }

        dateArray = dateArr;
        // init minute and hours
        this.changeTimePicker();

    },
    /**
     * 组件的方法列表
     */
    methods: {
        changeTimePicker: function(e) { // swapper UI
            // var today = new Date();

            //上舍入

            if (e && e.type != "tap") {
                if (e.detail.column == 0) {
                    dateIndex = e.detail.value;
                }
                coulmn = e.detail.column;
                value = e.detail.value;

            }
            // init hours and minutes
            switch (coulmn) {
                case (0):
                    {
                        hoursArray = [];
                        minutesArray = [];

                        switch (dateIndex) {
                            case (0):
                                {
                                    if (this.properties.HasNow) {
                                        hoursArray = ["现在"];
                                        minutesArray = ["现在"];
                                    } else {
                                        firstDayDate = timepicker.changeTimeHourMinutesWithMinutes(hoursArray, minutesArray, this.properties.FirstTimeMinuteLatter);
                                    }

                                };
                                break;
                            case (1):
                                {
                                    if (this.properties.HasNow) {
                                        firstDayDate = timepicker.changeTimeHourMinutesWithMinutes(hoursArray, minutesArray, this.properties.FirstTimeMinuteLatter);
                                    } else {
                                        var options = {
                                            hoursArray: hoursArray,
                                            minutesArray: minutesArray
                                        }
                                        timepicker.initBothHoursOrMiuteAsUsual(true, options);
                                    }

                                };
                                break;
                            default:
                                { //not first show date line
                                    var options = {
                                        hoursArray: hoursArray,
                                        minutesArray: minutesArray
                                    }
                                    timepicker.initBothHoursOrMiuteAsUsual(true, options);
                                }
                        }

                    };
                    break;
                case (1):
                    {

                        minutesArray = [];
                        switch (dateIndex) {
                            case (0):
                                {
                                    if (this.properties.HasNow) {
                                        hoursArray = ["现在"];
                                        minutesArray = ["现在"];
                                    } else {
                                        switch (value) {
                                            case (0):
                                                {
                                                    firstDayDate = timepicker.changeTimeHourMinutesWithMinutes(hoursArray, minutesArray, this.properties.FirstTimeMinuteLatter);
                                                };
                                                break;
                                            default:
                                                {
                                                    var options = {
                                                        hoursArray: hoursArray,
                                                        minutesArray: minutesArray
                                                    }
                                                    timepicker.initBothHoursOrMiuteAsUsual(false, options);
                                                };
                                                break;
                                        }
                                    }

                                };
                                break;
                            case (1):
                                {
                                    if (this.properties.HasNow) {
                                        switch (value) {
                                            case (0):
                                                {
                                                    hoursArray = [];
                                                    firstDayDate = timepicker.changeTimeHourMinutesWithMinutes(hoursArray, minutesArray, this.properties.FirstTimeMinuteLatter);
                                                };
                                                break;
                                            default:
                                                {
                                                    var options = {
                                                        // hoursArray: hoursArray,
                                                        minutesArray: minutesArray
                                                    }
                                                    timepicker.initBothHoursOrMiuteAsUsual(false, options);
                                                };
                                                break;
                                        }
                                    } else {
                                        var options = {
                                            hoursArray: hoursArray,
                                            minutesArray: minutesArray
                                        }
                                        timepicker.initBothHoursOrMiuteAsUsual(true, options);
                                    }


                                };
                                break;
                            default:
                                { //not first show date line
                                    var options = {
                                        hoursArray: hoursArray,
                                        minutesArray: minutesArray
                                    }
                                    timepicker.initBothHoursOrMiuteAsUsual(true, options);
                                }
                        }
                    };
                    break;
                default:
                    {


                    }
                    break;
            }

            // obj dateRange
            var objDateArray = [],
                objHoursArray = [],
                objMinutesArray = [];
            for (var i = 0; i < dateArray.length; i++) {
                var item = {
                    id: i,
                    value: dateArray[i]
                }
                objDateArray.push(item);
            };
            for (var i = 0; i < hoursArray.length; i++) {
                var hourValue = hoursArray[i].replace("点", "");
                var item = {
                    id: i,
                    value: hourValue
                }
                objHoursArray.push(item);
            };
            for (var i = 0; i < minutesArray.length; i++) {
                var minuteValue = minutesArray[i].replace("分", "");
                var item = {
                    id: i,
                    value: minuteValue
                }
                objMinutesArray.push(item);
            };
            // var 

            objectDateRange = [objDateArray, objHoursArray, objMinutesArray];

            this.setData({
                dateRange: [dateArray, hoursArray, minutesArray],
            });
        },
        confirmTimePicker: function(e) {
            // day
            var resultCoulmn1 = e.detail.value[0];
            // 
            // hour
            var resultCoulmn2 = e.detail.value[1];
            // minute
            var resultCoulmn3 = e.detail.value[2];
            var date = objectDateRange[0][resultCoulmn1].value;
            var hour = objectDateRange[1][resultCoulmn2].value;
            var minute = objectDateRange[2][resultCoulmn3].value;
            var operateTodayStamp;
            // 最终日期
            if(this.properties.HasNow){
                if (1 >= resultCoulmn1 ){
                    operateTodayStamp = firstDayDate.getTime() + parseInt(hour) * 60 * 60 * 1000 + parseInt(minute) * 60 * 1000;
                }else{
                    operateTodayStamp = firstDayDate.getTime() + (resultCoulmn1 -1) * 24 * 60 * 60 * 1000 + parseInt(hour) * 60 * 60 * 1000 + parseInt(minute) * 60 * 1000;
                }
                
            }else{
                operateTodayStamp = firstDayDate.getTime() + (resultCoulmn1) * 24 * 60 * 60 * 1000 + parseInt(hour) * 60 * 60 * 1000 + parseInt(minute) * 60 * 1000;
            };
            

            var isNow = date == "现在";
            selectDate = !isNow ? new Date(operateTodayStamp) : new Date();
            var confirmDate = !isNow ? date + " " + hour + ":" + this.getNumber(minute) : "时间:现在";

            this.setData({
                dateTime: confirmDate,
                ceateOrderNow: isNow
            });
            // console.log("PIKER selectDate = " + selectDate);
            this.properties.ComfrimYHDatePickerFunc({
                selectDate: selectDate,
                ceateOrderNow: isNow
            });
        },
        getNumber: function(num) {
            num = num + '';
            num = num.length == 2 ? num : ('0' + num);
            return num;
        }
    }
})